package pl.softech.chart;

import java.util.Collection;
import java.util.Date;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.data.Range;
import org.jfree.data.xy.DefaultHighLowDataset;
import org.jfree.data.xy.OHLCDataset;

import pl.softech.gpw.dao.RemoteDbBossaDao;
import pl.softech.gpw.model.FInstrument;

public class FreeChartExample {

    public static void main(String[] args) throws Exception {

        RemoteDbBossaDao bossaDao = new RemoteDbBossaDao();
        // MyTable<RowView> table = new MyTable<RowView>(RowView.class);
        // ArrayList<RowView> data = new ArrayList<RowView>();
        Collection<FInstrument> data = bossaDao.findeInstrumentByName("KGHM", "2011-05-01");
        Date[] date = new Date[data.size()];
        double[] high = new double[data.size()];
        double[] low = new double[data.size()];
        double[] open = new double[data.size()];
        double[] close = new double[data.size()];
        double[] volume = new double[data.size()];
        int index = 0;
        for (FInstrument f : data) {
            date[index] = f.getDate();
            high[index] = f.getHigh();
            low[index] = f.getLow();
            open[index] = f.getOpen();
            close[index] = f.getClose();
            volume[index] = f.getVolume();
            index++;
            // data.add(f.getRowView());
        }
        // table.setData(data);
        // JFrame f = Utils.getJFrame(table, "Simple Table", 1024, 768);
        // f.setVisible(true);

        Comparable<Object> seriesKey = new Comparable<Object>() {

            @Override
            public int compareTo(Object o) {
                return 0;
            }

            @Override
            public String toString() {

                return "Dupa";
            }
        };

        OHLCDataset dataset = new DefaultHighLowDataset(seriesKey, date, high, low, open, close, volume);
        JFreeChart chart = ChartFactory.createCandlestickChart("Sample Candle Chart", "Time", "Value", dataset, true);
        ((CandlestickRenderer) chart.getXYPlot().getRenderer()).setDrawVolume(false);
        chart.getXYPlot().getRangeAxis().setRange(new Range(175, 200));
        ChartFrame frame = new ChartFrame("First", chart);
        frame.pack();
        frame.setVisible(true);

    }

}
